package com.hx.petsever.impl;

        import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
        import com.hx.petsever.entity.*;
        import com.hx.petsever.mapper.GoodsMapper;
        import com.hx.petsever.mapper.OrderMapper;
        import com.hx.petsever.mapper.VipMapper;
        import com.hx.petsever.service.OrderService;
        import javafx.collections.MapChangeListener;
        import org.apache.ibatis.annotations.Param;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;

        import java.util.HashMap;
        import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    VipMapper vipMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public HashMap InsertOne(Orders orders, userOrder userOrder) {
        HashMap<String , Object> map = new HashMap<>();
        Goods goods = goodsMapper.getOneById(orders.getGoodid());
        if(goods.getNumber() < orders.getNumber())
         {
             map.put("msg","库存不足，余量为:"+goods.getNumber());
             map.put("state",-1);
         }
        else
        {
            orderMapper.InsertRelation(userOrder.getUserid(),userOrder.getOrderid());
            orderMapper.InsertOne(orders.getId(), orders.getName(), orders.getAddress(), orders.getPhone(), orders.getState(), orders.getGoodname(), orders.getPrice(),orders.getNumber(),orders.getGoodid());
            goods.setNumber(goods.getNumber()-orders.getNumber());
            goodsMapper.updateNumber(goods.getNumber(),goods.getId());
            map.put("state",1);
            map.put("msg","成功");
        }
        return map;
    }

    @Override
    public HashMap getOrderByState(String userid, Integer state) {
        List<Orders> orderByState = orderMapper.getOrderByState(userid, state);
        HashMap<String , Object> map =  new HashMap<>();
        map.put("data",orderByState);
        return map;
    }

    @Override
    public HashMap buyOne(String userid, String id , String price) {
        Vip vip = vipMapper.havaVip(userid);
        HashMap<String, Object> map = new HashMap<>();
        int p = Integer.parseInt(price);
        if(vip.getAccount() >= p)
        {
            Integer res = vip.getAccount() - p;
            vipMapper.updateAccount(userid,res);
            orderMapper.updateOrder(id,2);
            map.put("state","0");
            map.put("msg","成功");
        }else{
            map.put("state","0");
            map.put("msg","账户余额不足");
        }
        return map;
    }

    @Override
    public HashMap cancelOrder(String id) {
        Orders one = orderMapper.getOne(id);
        Goods oneById = goodsMapper.getOneById(one.getGoodid());
        orderMapper.updateOrder(id,-1);
        oneById.setNumber(oneById.getNumber()+one.getNumber());
        goodsMapper.updateNumber(oneById.getNumber(),oneById.getId());
        return null;
    }

    @Override
    public HashMap confirmOrder(String id) {
        orderMapper.updateOrder(id,4);
        return null;
    }

    @Override
    public HashMap applyCancelOrder(String id,@Param("reason") String reason) {
        orderMapper.updateOrder(id,-2);
        orderMapper.InsertReason(id,reason);
        return null;
    }

    @Override
    public HashMap getAll() {
        List<Orders> all = orderMapper.getAll();
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",all);
        return map;
    }

    @Override
    public HashMap deliver( @Param("id") String id) {
        orderMapper.updateOrder(id,3);
        HashMap<String, Object> map = new HashMap<>();
        return null;
    }

    @Override
    public String getReason(String id) {
        String reason = orderMapper.getReason(id);
        return reason;
    }

    @Override
    public HashMap backOrder(String id) {
        Orders one = orderMapper.getOne(id);
        Goods oneById = goodsMapper.getOneById(one.getGoodid());

        orderMapper.updateOrder(id,-3);
        oneById.setNumber(oneById.getNumber()+one.getNumber());

        goodsMapper.updateNumber(oneById.getNumber(),oneById.getId());
        String price = one.getPrice();

        String userid = orderMapper.getUserid(id);
        Vip vip = vipMapper.havaVip(userid);
        vipMapper.updateAccount(userid,vip.getAccount()+Integer.parseInt(price));

        return null;

    }

    @Override
    public HashMap Assess(Integer goodid, String orderid, String userid, String assess) {
        orderMapper.InsertAssess(goodid,orderid,userid,assess);
        return null;
    }

    @Override
    public HashMap haveAssess(String orderid) {
        GoodAssess goodAssess = orderMapper.haveAssessByOrderId(orderid);
        HashMap<String, Object> map = new HashMap<>();
        if(goodAssess != null)
        {
            map.put("state",1);
        }else  map.put("state",0);
        return map;
    }


}
